home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-02
/
prodpack.zip
/
DB4PPSRC.EXE
/
_TMPNAME.PRG
< prev
next >
Wrap
Text File
|
1993-05-04
|
4KB
|
141 lines
FUNCTION _TmpName && Returns a pseudo-random file root name
PARAMETER pc_ext
*--------------------------------------------------------------------
* NAME
* _TmpName - Returns a pseudo-random file root name.
*
* SYNOPSIS
* _TmpName( [.ext] )
*
* DESCRIPTION
* _TMPNAME() returns an pseudo-random string of
* digits suitable for use as a temporary file name.
* Eight digits (sometimes fewer) are returned.
* Successive calls to _TMPNAME() can be used to
* generate a series of unique file names.
*
* An optional file extension can be passed as an
* argument. If this is done, _TMPNAME will make
* sure that the file name it returns does not already
* exist within the current dBASE path setting.
*
* If either the DBTMP or TMP DOS environment variables
* are set, _TMPNAME() will use its value for a path
* prefix. If both are set, the DBTMP value is used.
*
* PARAMETERS
* pc_ext - optional file name extension. May optionally
* start with a ".", followed by up to three characters.
* (Note that some characters are not allowed in file names,
* depending on the specific operating system in use.)
*
* EXAMPLE
*
* lc_tmpfile = _TMPNAME(".TMP")
* * Possible return value: "87113336.TMP"
* USE master
* COPY TO (lc_tmpfile)
* * The file "87113336.TMP" would now exist
*
* LIMITATIONS
* If _TMPNAME() is used without the extension
* parameter, the FILE() function can be used to
* make certain that a created file name does not
* already exist.
*
* _TMPNAME() assumes the extension argument has
* only characters allowed in filenames.
*
* Note also that leading 0's will not be returned.
* If you desire exactly eight digits, this line:
* TRANSFORM( RAND(-1) * 100000000, "@L 99999999" )
* returns a random string of digits that is always
* eight characters long.
*
* SEE ALSO:
* RAND(), FILE()
*
*--------------------------------------------------------------------
PRIVATE lc_env, lc_ext, lc_prefix, lc_root, lc_slash, ;
ll_err, lh_chkit
IF LEFT( OS(), 3 ) = "DOS"
lc_slash = "\"
ELSE
lc_slash = "/"
ENDIF
lh_chkit = 0
lc_env = GETENV( "DBTMP" )
IF .NOT. ISBLANK( lc_env )
ll_err = .F.
ON ERROR ll_err = .T.
lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
lh_chkit = FCREATE( lc_prefix + [CHECKIT.OUT] )
IF lh_chkit > 0
IF FCLOSE( lh_chkit )
ERASE ( lc_prefix + [CHECKIT.OUT] )
ENDIF
ELSE
lc_env = ""
ll_err = .F.
ENDIF
ON ERROR
ENDIF
IF ISBLANK( m->lc_env )
lc_env = GETENV( "TMP" )
IF .NOT. ISBLANK( lc_env )
ll_err = .F.
ON ERROR ll_err = .T.
lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
lh_chkit = FCREATE( lc_prefix + [CHECKIT.OUT] )
IF lh_chkit > 0
IF FCLOSE( lh_chkit )
ERASE ( lc_prefix + [CHECKIT.OUT] )
ENDIF
ELSE
lc_env = ""
ll_err = .F.
ENDIF
ON ERROR
ENDIF
IF ISBLANK( m->lc_env )
lc_prefix = ""
ELSE
lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
ENDIF
ELSE
lc_prefix = IIF( RIGHT( lc_env, 1 ) = lc_slash, lc_env, lc_env + lc_slash )
ENDIF
IF PCOUNT() = 0
lc_root = m->lc_prefix + LTRIM( STR( RAND( -1 ) * 100000000, 8 ) )
RETURN( lc_root )
ELSE
IF .NOT. "." $ m->pc_ext
lc_ext = "." + m->pc_ext
ELSE
lc_ext = m->pc_ext
ENDIF
lc_ext = SUBSTR(m->lc_ext, 1, 4)
DO WHILE .T.
lc_root = LTRIM( STR( RAND( -1 ) * 100000000, 8 ) )
IF .NOT. FILE( m->lc_prefix + m->lc_root + m->lc_ext )
RETURN( m->lc_prefix + m->lc_root + m->lc_ext )
ENDIF
ENDDO
ENDIF
*-- EOF: _TmpName( [.ext] )